package com.gao.api.gate.quick.endpoint;

import io.netty.channel.ChannelOption;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.AttributeKey;

import java.net.SocketAddress;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

/**
 * <p></p>
 *
 * @author gaoshuangwen
 * @since 2022/01/23 15:51
 */
public class EndpointConfig {
    Map<AttributeKey<?>, ?> attributes;
    Map<ChannelOption<?>, ?> options;
    ChannelGroup channelGroup;
    LoggingHandler loggingHandler;
    LoopGroupConfig ioLoopGroupConfig;

    public EndpointConfig(Map<AttributeKey<?>, ?> attributes,
                          Map<ChannelOption<?>, ?> options,
                          ChannelGroup channelGroup,
                          LoggingHandler loggingHandler,
                          LoopGroupConfig ioLoopGroupConfig) {
        this.attributes = attributes;
        this.options = options;
        this.channelGroup = channelGroup;
        this.loggingHandler = loggingHandler;
        this.ioLoopGroupConfig = ioLoopGroupConfig;
    }

    public final Map<AttributeKey<?>, ?> attributes() {
        return attributes.isEmpty()?Collections.emptyMap():Collections.unmodifiableMap(attributes);
    }
    public final Map<ChannelOption<?>, ?> options() {
        return options.isEmpty()?Collections.emptyMap():Collections.unmodifiableMap(options);
    }
    public final ChannelGroup channelGroup() {
        return channelGroup;
    }
    public final LoggingHandler loggingHandler() {
        return loggingHandler;
    }
    public final LoopGroupConfig ioLoopGroupConfig() {
        return ioLoopGroupConfig;
    }
    protected static <K, V> Map<K, V> updateMap(Map<K, V> parentMap, Object key,  Object value) {
        if (parentMap.isEmpty()) {
            return value == null ? parentMap : Collections.singletonMap((K) key, (V) value);
        }
        else {
            Map<K, V> attrs = new HashMap<>(parentMap.size() + 1);
            attrs.putAll(parentMap);
            if (value == null) {
                attrs.remove(key);
            }
            else {
                attrs.put((K) key, (V) value);
            }
            return attrs;
        }
    }

}
